package com.ycwl.aiLive_2d.utils.jwtUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public final class StringUtil {

    public StringUtil() {
    }

    public static String a(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static Object convertEmpty(Object obj) {
        return obj == null ? "" : obj;
    }

    public static String reverseString(String s) {
        String[] split = s.split("-");
        String[] newArr = new String[split.length];
        for (int i = split.length - 1, j = 0; i >= 0; i--, j++) {
            newArr[j] = split[i];
        }
        split = newArr;
        StringBuilder st = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            st.append(split[i]).append("-");
        }
        String companyName;
        companyName = st.substring(0, st.lastIndexOf("-"));
        return companyName;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    /**
     * 去掉图片的base64的头部
     *
     * @param base64
     * @return
     */
    public static String baseurlPhotos(String base64) {
        return base64.substring(base64.indexOf(",") + 1);
    }

}